package com.sushe.service;

import com.sushe.dao.BedDao;
import com.sushe.dao.RoomDao;
import com.sushe.service.jiekou.RoomDeleteService;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class RoomDeleteServiceImpl implements RoomDeleteService {

    private RoomDao roomDao=new RoomDao();
    private BedDao bedDao=new BedDao();


    @Override
    public  void deleteRoom() throws SQLException, ClassNotFoundException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的宿舍房间号：");
        String roomNumber=sc.nextLine();
        //获取宿舍Id-roomId
        Integer roomId= roomDao.getRoomId(roomNumber);

        //在bed表中判断这个宿舍有没有学生入住，如果有则无法删除
        List<Integer> studentIdList=bedDao.StudentIn(roomId);
        //移除List中所有值为0的元素
        studentIdList.removeIf(num->num==0);
        if(studentIdList.size()>0&&studentIdList!=null){
            System.out.println("该宿舍有学生入住，无法删除!");
            System.out.println("该宿舍学生Id为：");
            System.out.println(studentIdList);
            return;
        }

        //执行到这里说明宿舍无学生入住，可以删除宿舍房间

        //首先删除bed床位信息
        bedDao.delete(roomId);
        //最后删除room房间信息
        roomDao.delete(roomId);

        System.out.println("删除成功！");
    }
}
